package me.pearjelly.filters.commons;

import me.pearjelly.filters.VisitFilter;
import edu.uci.ics.crawler4j.url.WebURL;

public class DomainFilter implements VisitFilter {
	private VisitFilter visitFilter;
	private String[] domains;

	public DomainFilter(String domain, VisitFilter visitFilter) {
		this.domains = new String[] { domain };
		this.visitFilter = visitFilter;
	}

	public DomainFilter(String[] domains, VisitFilter visitFilter) {
		this.domains = domains;
		this.visitFilter = visitFilter;
	}

	@Override
	public boolean filter(WebURL webURL) {
		String url = webURL.getURL().toLowerCase();
		for (String domain : domains) {
			if (!url.startsWith(domain)) {
				return false;
			}
		}
		return null == visitFilter || visitFilter.filter(webURL);
	}

}
